package zd.cornermemory.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import zd.cornermemory.R;
import zd.cornermemory.bean.CornerCode3;
import zd.cornermemory.bean.CornerCode3Sub;
import zd.cornermemory.bean.CornerCodeItem2;
import zd.cornermemory.bean.Cube;
import zd.cornermemory.utils.ClickUtils;
import zd.cornermemory.utils.TimerForCode;

/**
 * 角编码测速三
 */
public class CornerCodeCs3Activity extends BaseCodeActivity {
    private TextView text1;
    private TextView text2;
    private TextView text3;
    private TextView text4;
    private View view1;
    private View view2;
    private View view3;
    private View view4;
    private ImageView img;
    private TextView time;
    private TextView test_text;//当前题目标识 例如 2/10
    private int po;//当前题号
    private List<CornerCode3> cornerCodeReviewBeen;//题目集合
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_corner_code_cs3);
        setBackImage();
        text1 = findViewById(R.id.text1);
        text2 = findViewById(R.id.text2);
        text3 = findViewById(R.id.text3);
        text4 = findViewById(R.id.text4);
        view1 = findViewById(R.id.v1);
        view2 = findViewById(R.id.v2);
        view3 = findViewById(R.id.v3);
        view4 = findViewById(R.id.v4);
        img = findViewById(R.id.img);
        time = findViewById(R.id.time);
        test_text = findViewById(R.id.test_text);
        setErrorBg(view1);
        setErrorBg(view2);
        setErrorBg(view3);
        setErrorBg(view4);
        initCornerCode();
        initCode();
        show();
        view1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ClickUtils.isFastDoubleClick()) {
                    boolean istrue = (boolean) view1.getTag();
                    po++;
                    if (istrue) {
                        rightCount++;
                    }
                    mHandler.sendEmptyMessageDelayed(0, 200);
                }
            }
        });
        view2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ClickUtils.isFastDoubleClick()) {
                    boolean istrue = (boolean) view2.getTag();
                    po++;
                    if (istrue) {
                        rightCount++;
                    }
                    mHandler.sendEmptyMessageDelayed(0, 200);
                }

            }
        });
        view3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ClickUtils.isFastDoubleClick()) {
                    boolean istrue = (boolean) view3.getTag();
                    po++;
                    if (istrue) {
                        rightCount++;
                    }
                    mHandler.sendEmptyMessageDelayed(0, 200);
                }

            }
        });
        view4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ClickUtils.isFastDoubleClick()) {
                    boolean istrue = (boolean) view4.getTag();
                    po++;
                    if (istrue) {
                        rightCount++;
                    }
                    mHandler.sendEmptyMessageDelayed(0, 200);
                }

            }
        });

    }

    @Override
    public void setTimerText(String str) {
        time.setText(str);
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (TimerForCode.state == 3) {//已结束
                return;
            }
            show();
        }
    };

    Random r = new Random();
    int rightCount = 0;
    private void show(){
        if (po >= 10) {
            int r = rightCount;
            rightCount = 0;
            TimerForCode.state = 3;
            timerForCode.count2();
            new AlertDialog.Builder(this)
                    .setTitle(getString(R.string.alert))
                    .setMessage("答对"+r+"题 用时"+time.getText().toString())
                    .setCancelable(false)
                    .setNegativeButton("再试一次", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int i) {
                            TimerForCode.state = 1;
                            dialog.dismiss();
                            timerForCode.count2();
                            po = 0;
                            rightCount = 0;
                            initCode();
                            show();
                        }
                    })
                    .setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            TimerForCode.state = 3;
                            dialog.dismiss();
                        }
                    }).show();



            return;
        }
        test_text.setText("题目"+(po+1)+"/10");
        CornerCode3Sub cornerCodeAnswerBean = answerBeen.get(po);
        int picInt = cornerCodeAnswerBean.getCornerPic();
        img.setImageResource(picInt);
        List<Integer> has = new ArrayList<>();
        TextView text;
        View v;
        for(int i = 0;i<4;i++) {
            int random = getRandom(4, has);
            has.add(random);
            CornerCodeItem2 cornerPicAnswerBean = cornerCodeAnswerBean.getCornerCodeItems().get(random);
            boolean isTrue = cornerPicAnswerBean.isTrue();

            if (i == 0) {
                text = text1;
                v = view1;
            } else if (i == 1) {
                text = text2;
                v = view2;
            } else if (i == 2) {
                text = text3;
                v = view3;
            }else{
                text = text4;
                v = view4;
            }
            v.setTag(isTrue);
            text.setText(cornerPicAnswerBean.getCode());
            if(isTrue){
                setRightBg(v);
            }else{
                setErrorBg(v);
            }

        }


    }




    List<CornerCode3Sub> answerBeen = new ArrayList<>();
    private void initCode(){
        answerBeen.clear();
        List<Integer> has = new ArrayList<>();
        Random r = new Random();
        for(int i = 0;i<10;i++) {
            if (has.size() >= 8) {
                has.clear();
            }
            int random = getRandom(8, has);
            has.add(random);
            CornerCode3 cornerCodeReviewBean = cornerCodeReviewBeen.get(random);
            List<Integer> picInt = cornerCodeReviewBean.getCornerPic();
            List<String> codeList = cornerCodeReviewBean.getCodeStrList();
            int pos = r.nextInt(3);


            List<String> has2 = new ArrayList<>();
            List<CornerCodeItem2> cornerPicAnswerBeen = new ArrayList<>();

            String codeTrue = codeList.get(pos);
            cornerPicAnswerBeen.add(new CornerCodeItem2(codeTrue,true));
            has2.add(codeTrue);


            String cc = getCc(has2);
            cornerPicAnswerBeen.add(new CornerCodeItem2(cc,false));
            has2.add(cc);

            String cc2 = getCc(has2);
            cornerPicAnswerBeen.add(new CornerCodeItem2(cc2,false));
            has2.add(cc2);

            String cc3 = getCc(has2);
            cornerPicAnswerBeen.add(new CornerCodeItem2(cc3,false));


            answerBeen.add(new CornerCode3Sub(picInt.get(pos),cornerPicAnswerBeen));

        }





    }


    private List<String> ccList;

    private String getCc(List<String> cc){
        String res = "";
        Random random = new Random();

        while (true) {
            int p = random.nextInt(ccList.size());
            res = ccList.get(p);
            if (!cc.contains(res)) {
                break;
            }
        }




        return res;
    }

    private void initCornerCode(){
        Cube cube = new Cube();
        cube.init();
        cornerCodeReviewBeen = new ArrayList<>();
        cornerCodeReviewBeen.add(new CornerCode3(getCornerPic(0),(cube.getCornerLocation0())));
        cornerCodeReviewBeen.add(new CornerCode3(getCornerPic(1),(cube.getCornerLocation1())));
        cornerCodeReviewBeen.add(new CornerCode3(getCornerPic(2),(cube.getCornerLocation2())));
        cornerCodeReviewBeen.add(new CornerCode3(getCornerPic(3),(cube.getCornerLocation3())));
        cornerCodeReviewBeen.add(new CornerCode3(getCornerPic(4),(cube.getCornerLocation4())));
        cornerCodeReviewBeen.add(new CornerCode3(getCornerPic(5),(cube.getCornerLocation5())));
        cornerCodeReviewBeen.add(new CornerCode3(getCornerPic(6),(cube.getCornerLocation6())));
        cornerCodeReviewBeen.add(new CornerCode3(getCornerPic(7),(cube.getCornerLocation7())));

        ccList = new ArrayList<>();

        for (CornerCode3 cornerCode3 : cornerCodeReviewBeen) {
            ccList.addAll(cornerCode3.getCodeStrList());
        }

    }







}
